import { service } from '@/service/service'
import { ElMessage } from 'element-plus'
import { router } from '@/router'

export const useDownloadOrLookFile = async (address, flag) => {
  if (!address) {
    ElMessage.error('地址为空！')
    return
  }
  let fileName
  if (address.includes('/')) {
    fileName = address.substring(address.lastIndexOf('/') + 1)
  } else {
    fileName = address.substring(address.lastIndexOf('\\') + 1)
  }
  if (flag) {
    service.downloadFile({ path: address }, fileName)
  } else {
    const xmlData = await service.lookFile({ path: address })
    sessionStorage.setItem('xmlFileString', xmlData)
    const routerData = router.resolve({ name: 'newXmlFile' })
    window.open(routerData.href, '_blank')
  }
}
